package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectPlanDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.entity.ProjectPlans;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.mapper.ProjectPlanMapper;
import com.zzyl.service.NursingProjectPlanService;
import com.zzyl.vo.NursingProjectPlanVo;
import com.zzyl.vo.NursingProjectVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class NursingProjectPlanServiceImpl implements NursingProjectPlanService {
    @Autowired
    NursingProjectPlanMapper nursingProjectPlanMapper;
    @Autowired
    ProjectPlanMapper projectPlanMapper;


    @Override
    public PageResponse<NursingProjectPlanVo> selectByPage(Integer pageNum, Integer pageSize, String planName, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingProjectPlan> nursingProjectPlans = nursingProjectPlanMapper.selectByPage(planName, status);
        return PageResponse.of(nursingProjectPlans, NursingProjectPlanVo.class);

    }

    @Override
    @Transactional
    public void addNursingProjectPlan(NursingProjectPlanDto nursingProjectPlanDto) {
        NursingProjectPlan bean = BeanUtil.toBean(nursingProjectPlanDto, NursingProjectPlan.class);
        nursingProjectPlanMapper.addNursingProjectPlan(bean);
        Long id = bean.getId();
        List<ProjectPlans> projectPlans = nursingProjectPlanDto.getProjectPlans();
        if(!CollectionUtil.isEmpty(projectPlans)){
            projectPlans.forEach(empExpr -> empExpr.setPlanId(id));
            projectPlanMapper.addNursingProjectPlanDetail(projectPlans);
        }
    }

    @Override
    public void deleteNursingProject(Long id) {

        nursingProjectPlanMapper.deleteNursingProject(id);
        projectPlanMapper.deleteNursingProject(id);
    }

    @Override
    public NursingProjectPlanVo getById(Long id) {
        NursingProjectPlanVo byId = nursingProjectPlanMapper.getById(id);
        List<ProjectPlans> PlansList = projectPlanMapper.getById(id);
        byId.setProjectPlans(PlansList);
        return BeanUtil.toBean(byId, NursingProjectPlanVo.class);


    }

    @Override
    public void upDateNursingProject(NursingProjectPlanDto nursingProjectPlanDto) {
        NursingProjectPlan nursingProjectPlan = BeanUtil.toBean(nursingProjectPlanDto, NursingProjectPlan.class);
        nursingProjectPlanMapper.upDateNursingProject(nursingProjectPlan);
        projectPlanMapper.deleteNursingProject(nursingProjectPlan.getId());
        List<ProjectPlans> projectPlans = nursingProjectPlanDto.getProjectPlans();
        if(!CollectionUtil.isEmpty(projectPlans)){
            projectPlans.forEach(empExpr -> empExpr.setPlanId(nursingProjectPlan.getId()));
            projectPlanMapper.addNursingProjectPlanDetail(projectPlans);
        }
        projectPlanMapper.upDateNursingProject(nursingProjectPlan);


    }

    @Override
    public void updateStatus(Long id, Integer status) {
        nursingProjectPlanMapper.updateStatus(id, status);
    }

    @Override
    public List<NursingProjectPlanVo> getAll() {

        return nursingProjectPlanMapper.getAll();
    }


}
